package com.aurora.CETManagementSystem.controller;


import com.alibaba.fastjson.JSON;
import com.aurora.CETManagementSystem.mapper.TestLogMapper;
import com.aurora.CETManagementSystem.mapper.UserInfoMapper;
import com.aurora.CETManagementSystem.pojo.Account;
import com.aurora.CETManagementSystem.pojo.TestLog;
import com.aurora.CETManagementSystem.pojo.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aurora
 * @since 2021-02-18
 */
@Controller
public class TestlogController {

    @Resource
    TestLogMapper testLogMapper;

    @Resource
    UserInfoMapper userInfoMapper;



    //删除一条报名信息
    @RequestMapping("/admin/toSignups/deleteLog/{id}")
    public String deleteLog(@PathVariable("id") String id){
        HashMap<String, Object> map = new HashMap<>();
        map.put("cetnum",id);
        testLogMapper.deleteByMap(map);
        return "redirect:/admin/toSignups";
    }


    /**
     * 查询自己的报名记录
     * @return
     */
    @GetMapping("/student/selectSignUpLists")
    public String selectSignupListsForStudent(HttpSession session, Model model){
        Account account = (Account) session.getAttribute("account");
        UserInfo student = userInfoMapper.selectById(account.getId());
        List<TestLog> testLogs = testLogMapper.selectList(new QueryWrapper<TestLog>().eq("stunum", student.getStuNum()));
        model.addAttribute("testLogs",testLogs);
        return "student/signup_list";
    }

    /**
     * 从自己考试记录跳转到准考证页面
     * @return
     */
    @RequestMapping("/student/toTicket/{cetnum}")
    public String toPrint(Model model,HttpSession session,@PathVariable("cetnum") String cetnum){

        Account account = (Account) session.getAttribute("account");
        UserInfo student = userInfoMapper.selectById(account.getId());

        TestLog testLog = testLogMapper.selectOne(new QueryWrapper<TestLog>().eq("cetnum", cetnum));


        model.addAttribute("student",student);
        model.addAttribute("testLog",testLog);


        return "student/ticket";
    }


    /**
     * 获取所有考试记录的学校信息 供chartjs使用
     */
    @GetMapping("/student/schoolCounts")
    @ResponseBody
    public String selectTestSchoolName(){
        List<String> schoolNames = testLogMapper.selectSchoolNames();
        List<Map<String,Object>> maps = new ArrayList<>();
        for (String schoolName: schoolNames) {
            Integer count = testLogMapper.selectCount(new QueryWrapper<TestLog>().eq("testschool", schoolName));
            HashMap<String, Object> map = new HashMap<>();
            map.put("school_name", schoolName);
            map.put("school_count", count);
            maps.add(map);
        }
        System.out.println(maps);
        return JSON.toJSONString(maps);
    }


    /**
     * 根据姓名或者学号对学生进行查询
     * @return
     */
    @RequestMapping("/admin/testlog/search")
    public String studentSearch(String search,Model model){
        List<TestLog> testLogs = testLogMapper.selectList(new QueryWrapper<TestLog>().like("name", search).or().like("stunum", search));

        if(!testLogs.isEmpty()){
            model.addAttribute("testLogs", testLogs);
        }else {
            model.addAttribute("msg", "未查询到相关结果!");
        }

        return "admin/signups";
    }




}

